﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.Common;

namespace TRPOP.CMS.Model
{
    /// <summary>
    /// 系统变量
    /// </summary>
    public class SystemVarInfo
    {
        #region 私有变量
        private string m_VarKey;
        private string m_VarName;
        private string m_VarValue;
        private string m_VarType;
        private string m_VarDelType;
        #endregion

        #region 成员函数

        /// <summary>
        /// 变量Key
        /// </summary>
        public string VarKey
        {
            get
            {
                return m_VarKey;
            }
            set
            {
                m_VarKey = value;
            }
        }

        /// <summary>
        /// 变量名称
        /// </summary>
        public string VarName
        {
            get
            {
                return m_VarName;
            }
            set
            {
                m_VarName = value;
            }
        }

        /// <summary>
        /// 变量值
        /// </summary>
        public string VarValue
        {
            get
            {
                return m_VarValue;
            }
            set
            {
                m_VarValue = value;
            }
        }

        /// <summary>
        /// 变量类型
        /// </summary>
        public string VarType
        {
            get
            {
                return m_VarType;
            }
            set
            {
                m_VarType = value;
            }
        }

        /// <summary>
        /// 变量是否允许删除
        /// </summary>
        public string VarDelType
        {
            get
            {
                return m_VarDelType;
            }
            set
            {
                m_VarDelType = value;
            }
        }
        #endregion

        #region 构造函数
        public SystemVarInfo()
        {
            m_VarKey = string.Empty;
            m_VarName = string.Empty;
            m_VarValue = string.Empty;
            m_VarType = string.Empty;
            m_VarDelType = string.Empty;
        }
        public SystemVarInfo(string strVarKey, string strVarName, string strVarValue, string strVarType, string strVarDelType)
        {
            m_VarKey = strVarKey;
            m_VarName = strVarName;
            m_VarValue = strVarValue;
            m_VarType = strVarType;
            m_VarDelType = strVarDelType;
        }
        #endregion
    }

    /// <summary>
    /// 系统变量 帮助器
    /// </summary>
    public static class SystemVarInfoHelper
    {
        #region 全局属性 构造器 通用方法
        static List<SystemVarInfo> m_TemplateVars;
        static List<SystemVarInfo> m_SystemVars;
        static string m_SystemVarFileName;
        static string m_TemplateVarFileName;

        static object ObjLock = new object();

        /// <summary>
        /// 根据Key值 获取内容
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="oList"></param>
        /// <returns></returns>
        static SystemVarInfo GetVarByKey(string strKey, List<SystemVarInfo> oList)
        {
            if (oList != null && oList.Count > 0)
            {
                return oList.Find(oInfo => oInfo.VarKey.Equals(strKey, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据Key值 保存内容
        /// </summary>
        /// <param name="oSystemVarInfo"></param>
        /// <param name="oList"></param>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        static bool SetVarByKey(SystemVarInfo oSystemVarInfo, List<SystemVarInfo> oList, string strFileName)
        {
            if (oList == null || oList.Count <= 0 || oSystemVarInfo == null)
            {
                return false;
            }

            if (oList.Exists(oInfo => oInfo.VarKey.Equals(oSystemVarInfo.VarKey, StringComparison.OrdinalIgnoreCase)))
            {
                SystemVarInfo oTempInfo = oList.Find(oInfo => oInfo.VarKey.Equals(oSystemVarInfo.VarKey, StringComparison.OrdinalIgnoreCase));
                oTempInfo.VarDelType = oSystemVarInfo.VarDelType;
                oTempInfo.VarKey = oSystemVarInfo.VarKey;
                oTempInfo.VarName = oSystemVarInfo.VarName;
                oTempInfo.VarType = oSystemVarInfo.VarType;
                oTempInfo.VarValue = oSystemVarInfo.VarValue;
            }
            else
            {
                oList.Add(oSystemVarInfo);
            }

            SerializationHelper.Save(oList, strFileName);

            return true;
        }

        /// <summary>
        /// 根据Key值 保存内容
        /// </summary>
        /// <param name="oSystemVarInfo"></param>
        /// <param name="oList"></param>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        static bool SetVarByKey(SystemVarInfo oSystemVarInfo, List<SystemVarInfo> oList, string strFileName, bool blnIsValueAndName)
        {
            if (blnIsValueAndName == false)
            {
                return SetVarByKey(oSystemVarInfo, oList, strFileName);
            }

            if (oList == null || oList.Count <= 0 || oSystemVarInfo == null)
            {
                return false;
            }

            if (oList.Exists(oInfo => oInfo.VarKey.Equals(oSystemVarInfo.VarKey, StringComparison.OrdinalIgnoreCase)))
            {
                SystemVarInfo oTempInfo = oList.Find(oInfo => oInfo.VarKey.Equals(oSystemVarInfo.VarKey, StringComparison.OrdinalIgnoreCase));
                //oTempInfo.VarDelType = oSystemVarInfo.VarDelType;
                //oTempInfo.VarKey = oSystemVarInfo.VarKey;
                oTempInfo.VarName = oSystemVarInfo.VarName;
                //oTempInfo.VarType = oSystemVarInfo.VarType;
                oTempInfo.VarValue = oSystemVarInfo.VarValue;
            }
            else
            {
                oList.Add(oSystemVarInfo);
            }

            SerializationHelper.Save(oList, strFileName);

            return true;
        }
        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="oList"></param>
        /// <param name="strFileName"></param>
        /// <param name="GetDefaultParams"></param>
        /// <returns></returns>
        static List<SystemVarInfo> InitParam(List<SystemVarInfo> oList, string strFileName, Func<List<SystemVarInfo>> GetDefaultParams)
        {
            if (FileUtil.IsExist(strFileName))
            {
                oList = SerializationHelper.Load(typeof(List<SystemVarInfo>), strFileName) as List<SystemVarInfo>;
            }

            if (oList == null || oList.Count <= 0)
            {
                oList = GetDefaultParams();
                SerializationHelper.Save(oList, strFileName);
            }

            return oList;
        }

        static SystemVarInfoHelper()
        {
            m_SystemVarFileName = Common.Util.GetMapPath(SystemConst.ConfigRootName + SystemConst.SystemVar_SystemVarFileName);
            m_TemplateVarFileName = Common.Util.GetMapPath(SystemConst.ConfigRootName + SystemConst.SystemVar_TemplateVarFileName);
        }

        /// <summary>
        /// 重置所有变量
        /// </summary>
        static void ResetSystemVarInfoHelper()
        {
            m_TemplateVars = null;
            m_SystemVars = null;
        }
        #endregion

        #region  系统变量

        /// <summary>
        /// 所有的系统变量
        /// </summary>
        public static List<SystemVarInfo> SystemVars
        {
            get
            {
                if (m_SystemVars == null || m_SystemVars.Count <= 0)
                {
                    m_SystemVars = InitParam(m_SystemVars, m_SystemVarFileName, getDefaultSystemVars);
                }

                return m_SystemVars;
            }
            set
            {
                m_SystemVars = value;

                if (m_SystemVars != null && m_SystemVars.Count > 0)
                {
                    SerializationHelper.Save(m_SystemVars, m_SystemVarFileName);
                }
            }
        }

        /// <summary>
        /// 获取系统变量
        /// </summary>
        /// <param name="strkey"></param>
        /// <returns></returns>
        public static string GetSystemVarbyKey(string strkey)
        {
            SystemVarInfo oInfo = GetVarByKey(strkey, SystemVars);

            if (oInfo != null)
            {
                return oInfo.VarValue;
            }

            return string.Empty;
        }

        /// <summary>
        /// 获取默认的所有的系统变量
        /// </summary>
        /// <returns></returns>
        static List<SystemVarInfo> getDefaultSystemVars()
        {
            List<SystemVarInfo> oList = new List<SystemVarInfo>();

            oList.Add(new SystemVarInfo("SITE_URL", "网站网址", "http://localhost/", "text", "false"));
            oList.Add(new SystemVarInfo("SITE_NAME", "网站名称", "The Relentless Pursuit Of Perfection", "text", "false"));
            oList.Add(new SystemVarInfo("templatepath", "网站模板根目录", "/templates", "text", "false"));
            oList.Add(new SystemVarInfo("CahceSeconds", "默认缓存时间(秒)", "24000", "text", "false"));
            oList.Add(new SystemVarInfo("DefaultResourcePSN", "默认资源发布点", "{PSN:1}/resource", "PSN", "false"));
            oList.Add(new SystemVarInfo("DefaultResourcePSNURL", "默认资源发布URL", "{PSN-URL:1}/resource", "PSNURL", "false"));
            oList.Add(new SystemVarInfo("DefaultContentPSN", "默认内容发布点", "{PSN:1}/html", "PSN", "false"));
            oList.Add(new SystemVarInfo("DefaultContentPSNURL", "默认内容发布URL", "{PSN-URL:1}/html", "PSNURL", "false"));
            oList.Add(new SystemVarInfo("ContentPageLength", "自动分页单页内容长度", "20000", "text", "false"));
            oList.Add(new SystemVarInfo("listPageCount", "列表页前N页静态", "10", "text", "false"));
            oList.Add(new SystemVarInfo("listAutoPage", "列表页动态访问地址", "/publish/list.aspx?nodeid={nodeid}&page={page}", "text", "false"));
            oList.Add(new SystemVarInfo("ContentAutoPage", "内容页动态访问地址", "/publish/content.aspx?id={IndexID}&page={Page}", "text", "false"));
            oList.Add(new SystemVarInfo("InfoClickSubmit", "信息统计页面", "/publish/InfoClickSubmit.aspx", "text", "false"));
            oList.Add(new SystemVarInfo("Index_Name", "首页链接", "index.html", "text", "false"));
            oList.Add(new SystemVarInfo("NodeIndexPage", "结点首页", "/publish/list.aspx", "text", "false"));
            oList.Add(new SystemVarInfo("list_titlelength", "发布管理页标题长度", "40", "text", "false"));
            oList.Add(new SystemVarInfo("adminContentPageNum", "发布管理单页显示记录", "15", "text", "false"));
            oList.Add(new SystemVarInfo("PubPageListMax", "发布管理单页内容显示最大值", "30", "text", "false"));
            oList.Add(new SystemVarInfo("PageSearchURL", "搜索页链接", "http://localhost/publish/Search.aspx", "text", "false"));
            oList.Add(new SystemVarInfo("PageSearchTemplate", "搜索页模板", "/templates/default/search.html", "text", "false"));
            oList.Add(new SystemVarInfo("PagePromotTemplate", "信息提示页模板", "/templates/default/prompt.html", "text", "false"));
            oList.Add(new SystemVarInfo("SearchFormInputName", "搜索信息的form元素名", "q", "text", "false"));

            return oList;
        }

        #endregion

        #region  模板变量
        /// <summary>
        /// 所有的模板变量
        /// </summary>
        public static List<SystemVarInfo> TemplateVars
        {
            get
            {
                if (m_TemplateVars == null || m_TemplateVars.Count <= 0)
                {
                    m_TemplateVars = InitParam(m_TemplateVars, m_TemplateVarFileName, getDefaultTemplateVars);
                }

                return m_TemplateVars;
            }
            set
            {
                m_TemplateVars = value;

                if (m_TemplateVars != null && m_TemplateVars.Count > 0)
                {
                    SerializationHelper.Save(m_TemplateVars, m_TemplateVarFileName);
                }
            }
        }

        /// <summary>
        /// 获取模板变量
        /// </summary>
        /// <param name="strkey"></param>
        /// <returns></returns>
        public static string GetTemplateVarbyKey(string strkey)
        {
            SystemVarInfo oInfo = GetVarByKey(strkey, TemplateVars);

            if (oInfo != null)
            {
                return oInfo.VarValue;
            }

            return string.Empty;
        }

        static List<SystemVarInfo> getDefaultTemplateVars()
        {
            List<SystemVarInfo> oList = new List<SystemVarInfo>();

            oList.Add(new SystemVarInfo("SKIN_URL", "SKIN_URL", "http://localhost/skin/", "text", "false"));

            return oList;
        }
        #endregion

        #region 通用的方法 供外部调用
        /// <summary>
        /// 获取模板变量
        /// </summary>
        /// <param name="strVar"></param>
        /// <returns></returns>
        public static string GetVarValueForCMSVar(string strVar)
        {
            string strReturn = GetSystemVarbyKey(strVar);//GetVarValueFromConfig(strVar, "BaseParam");

            if (strReturn.IsEmpty())
            {
                strReturn = GetTemplateVarbyKey(strVar);
            }

            return strReturn;
        }

        /// <summary>
        /// 根据Key XML文件名（不带后缀） 删除变量
        /// </summary>
        /// <param name="strkey"></param>
        /// <param name="strXMLFileName"></param>
        /// <returns></returns>
        public static bool DelVarbyKey(string strkey, string strXMLFileName)
        {
            bool blnReturn = false;
            List<SystemVarInfo> oList = new List<SystemVarInfo>();
            string strFileName = Common.Util.GetMapPath(SystemConst.ConfigRootName + strXMLFileName + ".xml");

            if (FileUtil.IsExist(strFileName))
            {
                oList = SerializationHelper.Load(typeof(List<SystemVarInfo>), strFileName) as List<SystemVarInfo>;
            }

            if (oList == null || oList.Count <= 0)
                return blnReturn;

            SystemVarInfo oInfo = GetVarByKey(strkey, oList);

            if (oList.Contains(oInfo))
            {
                lock (ObjLock)
                {
                    if (oList.Contains(oInfo))
                    {
                        oList.Remove(oInfo);
                        SerializationHelper.Save(oList, strFileName);
                        ResetSystemVarInfoHelper();
                        blnReturn = true;
                    }
                }
            }

            return blnReturn;
        }

        /// <summary>
        /// 根据XML文件名（不带后缀） 获取变量集合 
        /// </summary>
        /// <param name="strParamKey"></param>
        /// <returns></returns>
        public static List<SystemVarInfo> GetVarsByParamKey(string strXMLFileName)
        {
            List<SystemVarInfo> oList = new List<SystemVarInfo>();
            string strFileName = Common.Util.GetMapPath(SystemConst.ConfigRootName + strXMLFileName + ".xml");

            if (FileUtil.IsExist(strFileName))
            {
                oList = SerializationHelper.Load(typeof(List<SystemVarInfo>), strFileName) as List<SystemVarInfo>;
            }

            return oList;
        }

        /// <summary>
        /// 保存变量
        /// </summary>
        /// <param name="oSystemVarInfo"></param>
        /// <param name="strXMLFileName"></param>
        /// <returns></returns>
        public static bool SaveVarByKey(SystemVarInfo oSystemVarInfo, string strXMLFileName, bool blnIsValueName)
        {

            List<SystemVarInfo> oList = new List<SystemVarInfo>();
            string strFileName = Common.Util.GetMapPath(SystemConst.ConfigRootName + strXMLFileName + ".xml");

            if (FileUtil.IsExist(strFileName))
            {
                oList = SerializationHelper.Load(typeof(List<SystemVarInfo>), strFileName) as List<SystemVarInfo>;
                return SetVarByKey(oSystemVarInfo, oList, strFileName, blnIsValueName);
            }

            return false;

        }
        #endregion
    }
}
